﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using KMF.Common;
using System.Linq;

namespace KMF.DAL
{
    public partial class NewsCategoryDb
    {
        public static List<NewsCategoryListAdminDto> GetAllParentListAdmin(int page, int itemsPerPage, ref int totalPage, ref int totalRecord)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Admin_NewsCategory_GetAllParent_Paging");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@Page", page));
                command.Parameters.Add(dao.CreateParameter("@ItemsPerPage", itemsPerPage));
                SqlParameter itemTotalPage = command.Parameters.Add("@TotalPage", SqlDbType.Int);
                itemTotalPage.Direction = ParameterDirection.Output;
                SqlParameter itemTotalRecord = command.Parameters.Add("@TotalRecord", SqlDbType.Int);
                itemTotalRecord.Direction = ParameterDirection.Output;

                var result = dao.GetDTOList<NewsCategoryListAdminDto>(ref command);
                totalPage = int.Parse(itemTotalPage.Value.ToString() == "" ? "0" : itemTotalPage.Value.ToString());
                totalRecord = int.Parse(itemTotalRecord.Value.ToString() == "" ? "0" : itemTotalRecord.Value.ToString());
                var Temp = new List<NewsCategoryListAdminDto>();
                var childListCate = GetAllChildListAdmin();

                if (result!=null && result.Any())
                {
                    foreach (NewsCategoryListAdminDto itemParent in result)
                    {

                        Temp.Add(itemParent); //add parent
                        childCate(childListCate, Temp, itemParent.Id, itemParent.Depth); //add child
                    }
                }

                return Temp;
            }
            finally
            {
                command.Connection.Close();
            }
        }

        public static void childCate(List<NewsCategoryListAdminDto> listChild, List<NewsCategoryListAdminDto> listParent, int id, int depth)
        {
            if (listChild != null && listChild.Any())
            {
                var childcate = listChild.Where(m => m.ParentId == id).ToList();
                var str = "";
                for (int i = 0; i < depth; i++)
                {
                    str += "— ";
                }
                if (childcate.Any())
                {
                    foreach (NewsCategoryListAdminDto item in childcate)
                    {
                        item.CateName = str + item.CateName;
                        listParent.Add(item);
                        childCate(listChild, listParent, item.Id, item.Depth);
                    }
                }
            }
        }

        public static List<NewsCategoryListAdminDto> GetAllChildListAdmin()
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Admin_NewsCategory_GetAllChild");
            try
            {
                var result = dao.GetDTOList<NewsCategoryListAdminDto>(ref command);
                return result;
            }
            finally
            {
                command.Connection.Close();
            }
        }
    }
}
